package br.com.mundodaprata.store.data.jpa;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import br.com.mundodaprata.store.data.ClientRepository;
import br.com.mundodaprata.store.model.Client;
import br.com.mundodaprata.store.model.User;

public class ClientRepositoryJPA extends NamedRepositoryJPA<Client>
									implements ClientRepository{

	public ClientRepositoryJPA(String queryName) {
		super(Client.class, queryName);
	}

	public ClientRepositoryJPA(Class<Client> type) {
		super(Client.class, "");
	}

	@Override
	public Client selectClient(User user) {
		if(user == null || user.getCode() == null){
			return null;
		}
		
		EntityManager em = getEntityManager();
		
		TypedQuery<Client> query = em.createNamedQuery("clientByUser", Client.class);
		query.setParameter("code", user.getCode());
		
		try {
			return query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	

}